
package uestc.bitman.edxapp.player;

import uestc.bitman.edxapp.EdxApp;
import uestc.bitman.edxapp.R;
import uestc.bitman.edxapp.downloading.DownloadingTaskAdapter;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import java.util.List;

public class VideoSelectLlistActivity extends Activity {

    private ListView mVideosList;

    private List<VideoDesciptor> mVideos;

    private VideoSelectListAdapter mAdapter;

    private String courseName, sectionName, chapterName;

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_list);

        mVideosList = (ListView) findViewById(R.id.list_section_videos);

        Intent intent = getIntent();
        mVideos = intent.getParcelableArrayListExtra("videos");
        courseName = intent.getStringExtra("course");
        sectionName = intent.getStringExtra("section");
        chapterName = intent.getStringExtra("chapter");

        mAdapter = new VideoSelectListAdapter(this, mVideos, new OnClickListener() {
            @Override
            public void onClick(View v) {
                int pos = (Integer) v.getTag();
                int status = EdxApp.startDownloadTask(mVideos.get(pos).getUrl(), sectionName,
                        chapterName, courseName, mVideos.get(pos).getTitle());
                if (status == DownloadingTaskAdapter.STATUS_DOWNLOADING) {
                    Toast.makeText(VideoSelectLlistActivity.this,
                            getString(R.string.label_downloading_exist), Toast.LENGTH_SHORT).show();
                    return;
                }
                Toast.makeText(VideoSelectLlistActivity.this,
                        getString(R.string.label_added_to_list), Toast.LENGTH_SHORT).show();
            }
        });

        mVideosList.setAdapter(mAdapter);
        mVideosList.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
                Intent intent = new Intent(VideoSelectLlistActivity.this,
                        WebViewPlayerActivity.class);
                intent.putExtra("video", mVideos.get(pos).getUrl());
                intent.putExtra("title", mVideos.get(pos).getTitle());
                startActivity(intent);
                finish();
            }
        });
    }
}
